/* ------------------------------------------------------------------ CLASS INTERFACE: string Tom Annau NOTE TO CNS 185 STUDENTS: This header file is written in C++. If you do not know C++, this file will be useless to you. The "string" object specified here is included merely for support of the "vector" and "matrix" objects and was not intended for you to use directly. You are of course free to use it, but no example code will be explicitly provided. It is not necessary to complete the assignments. ------------------------------------------------------------------- */ #ifndef string_class #define string_class #include #include /* ------------------------------------------------------------------- Constant definitions ------------------------------------------------------------------- */ const char EOS = '\0'; const char CR = '\n'; const char SPACE = ' '; const char TAB = '\t'; const char QUOTES = '"'; #define DEFAULT_DOUBLE_FORMAT "%G" const int NOT_FOUND = -1; /* ------------------------------------------------------------------- Class definition ------------------------------------------------------------------- */ class string { /* =================================================================== P U B L I C =================================================================== */ public: // Strings will automatically expand to accomodate any text placed // in them. You never need to worry about dynamic allocation. /* --------------------------------------------------------------- Constructors --------------------------------------------------------------- */ // Make an empty string string(); // Copy constructor string(const string& s); // Make a string with one character in it string(char c); // Make a string containing an integer string(int i); // Make a string containing a double; the default format is %G, // which makes scientific notation only if it has to string(double d, const char *format = DEFAULT_DOUBLE_FORMAT); // Start off a string with text in it from some character array string(const char *content); /* --------------------------------------------------------------- Destructor --------------------------------------------------------------- */ // Virtual destructor; deallocates free store space virtual ~string(void); /* --------------------------------------------------------------- Assignment operators --------------------------------------------------------------- */ // Copy value of another string; obliterates current value string& operator = (const string& s); // Copy single character into string; obliterates current value string& operator = (char c); // Copy character array into string; obliterates current value string& operator = (const char *c); /* --------------------------------------------------------------- Type casts, implicit and explicit --------------------------------------------------------------- */ // Cast to char* character array; allows for use in places which are // expecting to see a character array operator char*(void) const; // Return value of first number of various types found in string int to_int(void) const; float to_float(void) const; double to_double(void) const; /* --------------------------------------------------------------- Comparison operators --------------------------------------------------------------- */ // Comparison operators work the same way strcmp() works. Generally // the comparisons are done based on the ASCII character set rankings // Compare string to character array int operator == (const char *c) const; int operator != (const char *c) const; int operator < (const char *c) const; int operator > (const char *c) const; int operator <= (const char *c) const; int operator >= (const char *c) const; // Compare string to another string int operator == (const string& s) const; int operator != (const string& s) const; int operator < (const string& s) const; int operator > (const string& s) const; int operator <= (const string& s) const; int operator >= (const string& s) const; // Compare character array to string friend int operator == (const char *c, const string& s); friend int operator != (const char *c, const string& s); friend int operator < (const char *c, const string& s); friend int operator > (const char *c, const string& s); friend int operator <= (const char *c, const string& s); friend int operator >= (const char *c, const string& s); /* --------------------------------------------------------------- Concatenations --------------------------------------------------------------- */ // Add stuff to the end of the string string& operator += (char c); string& operator += (const string& s); string& operator += (const char *c); // Concatenate string operator + (char c) const; string operator + (const string& s) const; string operator + (const char *c) const; // For operations like: string s, t; s = "abcd" + t; friend string operator + (const char *c, const string& s); /* --------------------------------------------------------------- Whole string methods --------------------------------------------------------------- */ // Return length of the string int len(void) const; // Reset string to containing no characters void clear(void); // Does the string have no characters in it? int null(void) const; // Is the string just full of white space (tab, space, carriage return)? int blank(void) const; // Return the first character of the string, by reference char& first_char(void) const; // Return the last character of the string, by reference char& last_char(void) const; // Drop the first n characters from the string (default n=1) void drop_first_char(int n = 1); // Drop the last n characters from the string (default n=1) void drop_last_char(int n = 1); // Convert all letters in the string to lower case void lowerfy(void); // Convert all letters in the string to upper case void upperfy(void); // Remove white space from the beginning and end of the string void strip_spaces(void); // Remove from string the nth occurrence of the string 's' (default n=1) void remove(const string& s, int occurrence = 1); // Substitute all occurences of search_string with replacement void substitute(const string& search_string, const string& replacement); /* --------------------------------------------------------------- Substring methods --------------------------------------------------------------- */ // Return character at indexed position, by reference char& operator [] (int position) const; // Return string from first_pos to second_pos; if second_pos is // left off, returns string from first_pos to end of string string middle(int first_pos, int second_pos = NOT_FOUND) const; // Return first n characters of string string first(int characters) const; // Return last n characters of string string last(int characters) const; // Return string without characters from first_position to second_position string without(int first_position, int second_position) const; // Return the frequency of occurence of the substring s int frequency(const string& s) const; // Return the position of the nth occurence of substring s (default n=1) int position(const string& s, int occurrence = 1) const; /* --------------------------------------------------------------- Field operations --------------------------------------------------------------- */ // The following methods are useful if you string has fields. // Fields are text separated by white space. For example: the // string "field0 field1 field2" contains three fields. Fields can // contain white space if they are surrounded by quotes. If the // string starts off with white space, the first field is considered // to be null: the string " field1" contains two fields, a null string // as field #0 and "field1" as field #1. Note that fields are indexed // like arrays, i.e., from index zero to index (N-1) for N fields. // Return field indexed by field_number. Parameter starting_pos // allows the field counting to begin from some character other // than the first in the string string field(int field_number, int starting_pos = 0) const; // Return the number of fields in the string int fields(void) const; // Return field boundaries for field at a particular index. // Boundaries are returned in passed by reference variables. // starting_pos has the same meaning as in method field() above void field_boundaries(int field_number, int& start, int& end, int starting_pos = 0) const; // Return number of field index at a particular string position int field_of_position(int position) const; // Make sure string is a legal field (put in quotes if necessary) string encapsulate(void) const; /* --------------------------------------------------------------- Stream methods --------------------------------------------------------------- */ // Output string in binary format to ostream friend ostream& operator < (ostream& out, const string& s); // Input string in binary format from istream friend istream& operator > (istream& in, string& s); // Output string in ASCII format to ostream friend istream& operator >> (istream& in, string& s); // Input string in ASCII format from istream // Carriage return terminates input unless there is an unmatched // quote pending. EOF terminates input regardless. friend ostream& operator << (ostream& out, const string& s); /* =================================================================== P R O T E C T E D =================================================================== */ protected: char *character; int length; int blocks_allocated; void set_length(int new_length); }; /* =================================================================== I n l i n e f u n c t i o n s =================================================================== */ /* ------------------------------------------------------------------- Constructors and destructor ------------------------------------------------------------------- */ inline string::string() { blocks_allocated = 0; set_length(0); } inline string::string(const string& s) { blocks_allocated = 0; operator =(s); } inline string::string(char c) { blocks_allocated = 0; operator =(c); } inline string::string(const char* content) { blocks_allocated = 0; operator =(content); } /* ------------------------------------------------------------------- Type casts ------------------------------------------------------------------- */ inline string::operator char*(void) const { return character; } inline int string::to_int(void) const { return atoi(character); } inline float string::to_float(void) const { return (float) atof(character); } inline double string::to_double(void) const { return atof(character); } /* ------------------------------------------------------------------- Comparison operators ------------------------------------------------------------------- */ inline int string::operator == (const char *c) const { return strcmp(character, c) == 0; } inline int string::operator != (const char *c) const { return strcmp(character, c) != 0; } inline int string::operator < (const char *c) const { return strcmp(character, c) < 0; } inline int string::operator > (const char *c) const { return strcmp(character, c) > 0; } inline int string::operator <= (const char *c) const { return strcmp(character, c) <= 0; } inline int string::operator >= (const char *c) const { return strcmp(character, c) >= 0; } inline int string::operator == (const string& s) const { return strcmp(character, s.character) == 0; } inline int string::operator != (const string& s) const { return strcmp(character, s.character) != 0; } inline int string::operator < (const string& s) const { return strcmp(character, s.character) < 0; } inline int string::operator > (const string& s) const { return strcmp(character, s.character) > 0; } inline int string::operator <= (const string& s) const { return strcmp(character, s.character) <= 0; } inline int string::operator >= (const string& s) const { return strcmp(character, s.character) >= 0; } inline int operator == (const char *c, const string& s) { return strcmp(c, s.character) == 0; } inline int operator != (const char *c, const string& s) { return strcmp(c, s.character) != 0; } inline int operator < (const char *c, const string& s) { return strcmp(c, s.character) < 0; } inline int operator > (const char *c, const string& s) { return strcmp(c, s.character) > 0; } inline int operator <= (const char *c, const string& s) { return strcmp(c, s.character) <= 0; } inline int operator >= (const char *c, const string& s) { return strcmp(c, s.character) >= 0; } /* ------------------------------------------------------------------- Concatenations ------------------------------------------------------------------- */ inline string string::operator + (char c) const return r(*this); { r += c; } inline string string::operator + (const char *c) const return r(*this); { r += c; } inline string string::operator + (const string& s) const return r(*this); { r += s.character; } inline string& string::operator += (const string& s) { return *this += s.character; } inline string operator + (const char *c, const string& s) return r(c); { r += s; } /* ------------------------------------------------------------------- Whole string operations ------------------------------------------------------------------- */ inline int string::len(void) const { return length; } inline void string::clear(void) { *character = EOS; length = 0; } inline int string::null(void) const { return (length == 0); } inline char& string::first_char(void) const { return *character; } inline char& string::last_char(void) const { return *(character + length - 1); } /* ------------------------------------------------------------------- Substring operations ------------------------------------------------------------------- */ inline char& string::operator [] (int position) const { #ifdef RANGE_CHECKING if (position < 0 || position >= length) { cerr << "ERROR: Attempt to access illegal character in string\n"; return *((char *) NULL); } else return *(character + position); #endif #ifndef RANGE_CHECKING return *(character + position); #endif } inline string string::first(int characters) const return r; { if (characters > 0) r = middle(0, characters - 1); } inline string string::last(int characters) const return r; { if (characters > 0) r = middle(length - characters); } inline string string::without(int first_position, int second_position) const return r; { if (first_position >= 0 && second_position < length && first_position < second_position) r = first(first_position) + last(length - second_position - 1); } /* ------------------------------------------------------------------- Stream functions ------------------------------------------------------------------- */ inline ostream& operator << (ostream& out, const string& s) { return out << s.character; } inline ostream& operator < (ostream& out, char c) { return out.write(&c, sizeof(char)); } inline istream& operator > (istream& in, char& c) { return in.read(&c, sizeof(char)); } inline ostream& operator < (ostream& out, short w) { return out.write(&w, sizeof(short)); } inline istream& operator > (istream& in, short& w) { return in.read(&w, sizeof(short)); } inline ostream& operator < (ostream& out, int i) { return out.write(&i, sizeof(int)); } inline istream& operator > (istream& in, int& i) { return in.read(&i, sizeof(int)); } inline ostream& operator < (ostream& out, long l) { return out.write(&l, sizeof(long)); } inline istream& operator > (istream& in, long& l) { return in.read(&l, sizeof(long)); } inline ostream& operator < (ostream& out, float f) { return out.write(&f, sizeof(float)); } inline istream& operator > (istream& in, float& f) { return in.read(&f, sizeof(float)); } inline ostream& operator < (ostream& out, double d) { return out.write(&d, sizeof(double)); } inline istream& operator > (istream& in, double& d) { return in.read(&d, sizeof(double)); } #endif